home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / WASTE Object Handlers 1.2.6.sit / WASTE Object Handlers 1.2.6 / Handler Source / WE_hfs_Handler.c next >
Text File  |  1997-09-08  |  9KB  |  327 lines

  1. // File Object Handler for the WASTE Text Engine
  2. // by Michael F. Kamprath, kamprath@kagi.com
  3. // maintenance by John C. Daub, hsoi@eden.com
  4. //
  5. // v1.0, 12 March 1995
  6. //
  7. // v1.1, 1 April.  Fixed a a bug with draging hard drives to a WASTE instance
  8. //                 and a bix with insert file objects via InsertFileRefFromFSSpec()
  9. //
  10. // v1.2, 28 March 1996.    Minor touch-ups and updates. Added more precompiler directives.
  11. //                        Added compatability with WASTE 1.2a5.
  12. //
  13. // v1.2.2, 16 July 1996. Added compatability with WASTE 1.2 and CW9
  14. //                         Restructured installation routines
  15. //
  16. // v1.2.6, 8 Sept 1996. Fixed bugs in HandlDrawHFS and SizeHFSObject where the Handle
  17. //                        was being dereferenced without being locked.
  18.  
  19.  
  20. #ifndef __ICONS__
  21. #include <Icons.h>
  22. #endif
  23. #ifndef __DRAG__
  24. #include <Drag.h>
  25. #endif
  26. #ifndef __LOWMEM__
  27. #include <LowMem.h>
  28. #endif
  29. #ifndef _WASTE_
  30. #include "WASTE.h"
  31. #endif
  32.  
  33. #ifndef _WASTEOBJECTS_
  34.     #include "WASTE_Objects.h"
  35. #endif
  36.  
  37. #include "WE_hfs_Handler.h"
  38. #include "GetFileIcon.h"
  39. #include "SendFinderOpen.h"
  40.  
  41. #ifndef true
  42. #define true 1
  43. #endif
  44.  
  45. #ifndef false
  46. #define false 0
  47. #endif
  48.  
  49. // Local Functions
  50. static void        SizeHFSObject( Point *objectSize, HFSFlavor **theHFS );
  51.  
  52.  
  53. //
  54. // InstallHFSObject()
  55. //        Installs the HFS Object handler into WASTE.
  56. //
  57.  
  58. OSErr    InstallHFSObject( WEReference theWE )
  59. {
  60.     OSErr    iErr;
  61.  
  62. #ifdef __cplusplus
  63.     static WENewObjectUPP        newHFSUPP = NewWENewObjectProc(HandleNewHFS);
  64.     static WEDisposeObjectUPP     disposeHFSUPP = NewWEDisposeObjectProc(HandleDisposeHFS);
  65.     static WEDrawObjectUPP        drawHFSUPP = NewWEDrawObjectProc(HandleDrawHFS);
  66.     static WEClickObjectUPP        clickHFSUPP = NewWEClickObjectProc(HandleClickHFS);
  67. #else
  68.     static WENewObjectUPP        newHFSUPP = NULL;
  69.     static WEDisposeObjectUPP     disposeHFSUPP = NULL;
  70.     static WEDrawObjectUPP        drawHFSUPP = NULL;
  71.     static WEClickObjectUPP        clickHFSUPP = NULL;
  72.     
  73.     if ( newHFSUPP == NULL )
  74.         newHFSUPP = NewWENewObjectProc(HandleNewHFS);
  75.     if ( disposeHFSUPP == NULL )
  76.         disposeHFSUPP = NewWEDisposeObjectProc(HandleDisposeHFS);
  77.     if ( drawHFSUPP == NULL )    
  78.         drawHFSUPP = NewWEDrawObjectProc(HandleDrawHFS);
  79.     if ( clickHFSUPP == NULL )
  80.         clickHFSUPP = NewWEClickObjectProc(HandleClickHFS);
  81. #endif
  82.  
  83.     if ( newHFSUPP != NULL )
  84.         iErr = WEInstallObjectHandler(flavorTypeHFS, weNewHandler, (UniversalProcPtr)newHFSUPP, theWE);
  85.     else
  86.         iErr = weUnknownObjectTypeErr;
  87.     if (iErr) return(iErr);
  88.     
  89.     if ( disposeHFSUPP != NULL )
  90.         iErr = WEInstallObjectHandler(flavorTypeHFS, weDisposeHandler, (UniversalProcPtr)disposeHFSUPP, theWE);
  91.     else
  92.         iErr = weUnknownObjectTypeErr;
  93.     if (iErr) return(iErr);
  94.     
  95.     if ( drawHFSUPP != NULL)
  96.         iErr = WEInstallObjectHandler(flavorTypeHFS, weDrawHandler, (UniversalProcPtr)drawHFSUPP, theWE);
  97.     else
  98.         iErr = weUnknownObjectTypeErr;
  99.     if (iErr) return(iErr);
  100.     
  101.     if ( clickHFSUPP != NULL )
  102.         iErr = WEInstallObjectHandler(flavorTypeHFS, weClickHandler, (UniversalProcPtr)clickHFSUPP, theWE);
  103.     else
  104.         iErr = weUnknownObjectTypeErr;
  105.     if (iErr) return(iErr);
  106.     
  107.     return(noErr);
  108. }
  109.  
  110. //
  111. // New Object Handler for HFS Objects
  112. //
  113. pascal OSErr    HandleNewHFS(Point *defaultObjectSize,WEObjectReference objectRef)
  114. {
  115.     HFSFlavor    **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  116.  
  117.     // First size the object as WASTE requires
  118.     SizeHFSObject( defaultObjectSize, theHFS);
  119.     
  120.     // Set the object refcon to 0.  This is for safety
  121.     // as later we use the refcon to store the icon handle
  122.     // for this object.
  123.     WESetObjectRefCon( objectRef, 0 );
  124.     
  125.     return(noErr);
  126. }
  127.  
  128. //
  129. // Dispose Object handler for HFS Objects
  130. //
  131. pascal OSErr    HandleDisposeHFS(WEObjectReference objectRef )
  132. {
  133.     Handle    theHFS = WEGetObjectDataHandle(objectRef);
  134.     Handle    iconCache = (Handle)WEGetObjectRefCon( objectRef );
  135.  
  136.     // If the object has an icon handle loaded, dispose of it.
  137.     if (iconCache != 0)
  138.     {
  139.         DisposeIconSuite( iconCache, true );
  140.     }
  141.     
  142.     // Dispos of the HFS data
  143.     DisposeHandle(theHFS);
  144.  
  145.     return(MemError());
  146. }
  147.  
  148. //
  149. // Draw Object Handler for HFS objects
  150. //
  151.  
  152. pascal OSErr    HandleDrawHFS(Rect *destRect, WEObjectReference objectRef )
  153. {
  154. HFSFlavor    **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  155. Handle        iconCache = (Handle)WEGetObjectRefCon( objectRef );
  156. FontInfo    fInfo;
  157. GrafPtr        thePort;
  158. Rect        iconRect;
  159. OSErr        iErr;
  160. short        oldTextFont, oldTextSize, oldTextFace;
  161. short        sysTextFont = LMGetSysFontFam();
  162. short        sysTextSize = LMGetSysFontSize();
  163. char        state;
  164.  
  165.     // if the object's refcon did not contain an icon handle, load one.
  166.     if (iconCache == 0)
  167.     {
  168.         state = HGetState((Handle)theHFS);
  169.         HLockHi( (Handle)theHFS );
  170.         if ( ((*theHFS)->fileCreator=='MACS')&&((*theHFS)->fileType=='fold') )
  171.             iErr = GetIconSuite(&iconCache,genericFolderIconResource,svAllAvailableData);
  172.         else if ( ((*theHFS)->fileCreator=='MACS')&&((*theHFS)->fileType=='disk') )
  173.         {
  174.             iErr = GetIconSuite(&iconCache,genericHardDiskIconResource,svAllAvailableData);
  175.         }
  176.         else
  177.         {
  178.             iErr = GetFileIcon( &(*theHFS)->fileSpec, svAllAvailableData, &iconCache );
  179.             
  180.             if (iErr)
  181.                 iErr = GetIconSuite(&iconCache,genericDocumentIconResource,svAllAvailableData);
  182.         }    
  183.         HSetState((Handle)theHFS, state );
  184.         if (iErr)
  185.             return(iErr);
  186.         WESetObjectRefCon( objectRef, (long)iconCache );
  187.     }
  188.     
  189.     // Determine the icon's rectangle and plot it
  190.     SetRect( &iconRect, (destRect->right + destRect->left)/2 - 16, destRect->top,
  191.                         (destRect->right + destRect->left)/2 + 16, destRect->top + 32 );
  192.     iErr = PlotIconSuite(&iconRect,(IconAlignmentType)(atVerticalCenter + atHorizontalCenter),
  193.                                             (IconTransformType)ttNone,    iconCache);
  194.                                             
  195.     // Draw the file's title.  First save old font info for port.
  196.     GetPort(&thePort);
  197.     oldTextFont = thePort->txFont;
  198.     oldTextSize = thePort->txSize;
  199.     oldTextFace = thePort->txFace;
  200.     
  201.     TextFont( sysTextFont );
  202.     TextSize( sysTextSize );
  203.     TextFace( 0 );
  204.     GetFontInfo(&fInfo);
  205.     
  206.     MoveTo( destRect->left + 1, destRect->bottom - fInfo.descent - fInfo.leading );
  207.     state = HGetState((Handle)theHFS);
  208.     HLock((Handle)theHFS);
  209.     DrawString( (*theHFS)->fileSpec.name );
  210.     HSetState((Handle)theHFS, state);
  211.     
  212.     TextFont( oldTextFont );
  213.     TextSize( oldTextSize );
  214.     TextFace( oldTextFace );
  215.     
  216.     return( iErr );
  217. }
  218.  
  219. //
  220. // Click Handler for HFS Objects.  
  221. //        Will send the finder an open selection apple event when object is clicked.
  222. //
  223.  
  224.  
  225. pascal Boolean    HandleClickHFS(    Point hitPt, 
  226.                                     short modifiers, 
  227.                                     long clickTime, 
  228.                                     WEObjectReference objectRef)
  229. {
  230. #pragma unused (hitPt, clickTime)
  231.  
  232. HFSFlavor    **theHFS;
  233. char        state;
  234.  
  235.     if (modifiers & 0x0001)         // look for double-clicks
  236.     {
  237.         theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  238.         
  239.         state = HGetState((Handle)theHFS);
  240.         HLockHi( (Handle)theHFS );
  241.         SendFinderOpenAE(&(*theHFS)->fileSpec);
  242.         HSetState((Handle)theHFS, state );
  243.         
  244.         return(true);
  245.     }
  246.     else
  247.         return(false);
  248. }
  249.  
  250. //
  251. // SizeHFSObject()
  252. //        Used to return the display size of an HFS object
  253. //
  254. static void    SizeHFSObject( Point *objectSize, HFSFlavor **theHFS )
  255. {
  256. FontInfo    fInfo;
  257. GrafPtr        thePort;
  258. short        oldTextFont, oldTextSize, oldTextFace;
  259. short        strWidth, strHeight;
  260. short        sysTextFont = LMGetSysFontFam();
  261. short        sysTextSize = LMGetSysFontSize();
  262. char        state;
  263.  
  264.     GetPort(&thePort);
  265.     oldTextFont = thePort->txFont;
  266.     oldTextSize = thePort->txSize;
  267.     oldTextFace = thePort->txFace;
  268.     
  269.     TextFont( sysTextFont );
  270.     TextSize( sysTextSize );
  271.     TextFace( 0 );
  272.     
  273.     state = HGetState((Handle)theHFS);
  274.     HLock((Handle)theHFS);
  275.     strWidth = StringWidth( (*theHFS)->fileSpec.name ) + 4;
  276.     HSetState((Handle)theHFS, state);
  277.     GetFontInfo(&fInfo);
  278.     strHeight = fInfo.ascent + fInfo.descent + fInfo.leading;
  279.     
  280.     TextFont( oldTextFont );
  281.     TextSize( oldTextSize );
  282.     TextFace( oldTextFace );
  283.  
  284.     objectSize->h = ( strWidth > 32 ) ? strWidth : 32 ;
  285.     objectSize->v = 32 + strHeight + 1;    
  286. }
  287.  
  288. //
  289. // InsertFileRefFromFSSpec()
  290. //        Inserts a file object into a WASTE instance.
  291. //
  292.  
  293. OSErr    InsertFileRefFromFSSpec( FSSpec *theFile, WEReference theWE )
  294. {
  295. FInfo        fndrInfo;
  296. HFSFlavor    **theHFS;
  297. Point        theSize;
  298. OSErr        iErr;
  299. char        state;
  300.  
  301.     theHFS = (HFSFlavor**)NewHandleClear( sizeof(HFSFlavor) );
  302.     if (theHFS)
  303.     {
  304.         // First construct the HFSFlavor object from the FSSpec
  305.         iErr = FSpGetFInfo(theFile,&fndrInfo);
  306.         if (iErr)
  307.         {
  308.             DisposeHandle( (Handle)theHFS );
  309.             return(iErr);
  310.         }
  311.         
  312.         state = HGetState((Handle)theHFS);
  313.         HLock( (Handle)theHFS );
  314.         (*theHFS)->fileType = fndrInfo.fdType;
  315.         (*theHFS)->fileCreator = fndrInfo.fdCreator;
  316.         (*theHFS)->fdFlags = fndrInfo.fdFlags;
  317.         (*theHFS)->fileSpec = (*theFile);
  318.         HSetState((Handle)theHFS, state );
  319.         
  320.         SizeHFSObject( &theSize, theHFS );
  321.         
  322.         // Now insert it into the WEReference
  323.         WEInsertObject( flavorTypeHFS, (Handle)theHFS, theSize, theWE );
  324.     }
  325.     return(noErr);
  326. }
  327.